package com.shenmazong.serverswaggerdemo.controller;

import com.shenmazong.serverswaggerdemo.pojo.ResponseResult;
import com.shenmazong.serverswaggerdemo.pojo.TbUser;
import io.swagger.annotations.*;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.UUID;

@RestController
@Slf4j
@CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
@Api(tags = "用户管理相关接口")
public class UserController {

    @GetMapping(value = "/")
    public ResponseResult index() {
        ResponseResult responseResult = ResponseResult.SUCCESS();
        return responseResult;
    }

    // http://localhost:8000/swagger-ui.html

    @ApiOperation(value="获取用户信息", notes="根据用户ID获取用户信息", produces="application/json")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "Long"),
    })
    @GetMapping(value = "/getUser")
    public ResponseResult getUser(@RequestParam("id") Integer id) {
        ResponseResult responseResult = ResponseResult.SUCCESS();
        HashMap<String, Object> dataMap = new HashMap<>();

        log.info("id=" + id);
        dataMap.put("userId", id);
        dataMap.put("userName", "武松");

        responseResult.setResult(dataMap);
        return responseResult;
    }

    @ApiOperation(value="获取令牌", notes="随机获取用户令牌", produces="application/json")
    @PostMapping(value = "/getToken")
    public ResponseResult getToken() {
        ResponseResult responseResult = ResponseResult.SUCCESS();
        HashMap<String, Object> dataMap = new HashMap<>();

        String token = UUID.randomUUID().toString();
        log.info("token=" + token);
        dataMap.put("token", token);

        responseResult.setResult(dataMap);
        return responseResult;
    }

    @ApiOperation(value="增加用户", notes="向系统增加用户", httpMethod = "POST")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "userName", value = "用户昵称", required = true, dataType = "String"),
            @ApiImplicitParam(name = "userAge", value = "用户年龄", required = true, dataType = "int")
    })
    @ApiResponses({
            @ApiResponse(code=1000,message="请求成功"),
            @ApiResponse(code=4001,message="请求参数没填好"),
            @ApiResponse(code=4002,message="请求路径没有或页面跳转路径不对")
    })
    @PostMapping(value = "/addUser")
    public ResponseResult addUser(@RequestParam("userName") String userName,
                                  @RequestParam("userAge") Integer userAge) {
        ResponseResult responseResult = ResponseResult.SUCCESS();
        HashMap<String, Object> dataMap = new HashMap<>();

        String token = UUID.randomUUID().toString();
        log.info("token=" + token);
        dataMap.put("token", token);
        dataMap.put("userName", userName);
        dataMap.put("userAge", userAge);

        responseResult.setResult(dataMap);
        return responseResult;
    }

    @PostMapping(value = "/addUserByObj")
    public ResponseResult addUserByObj(@RequestBody TbUser tbUser) {
        ResponseResult responseResult = ResponseResult.SUCCESS();

        responseResult.setResult(tbUser);
        return responseResult;
    }
}
